//
//  MineRefundListViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/17.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "MineRefundListViewController.h"
#import "MineOrderTableViewCell.h"
#import "OrderModel.h"
#import "RefundDetailViewController.h"

@interface MineRefundListViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<OrderModel *> *tableArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation MineRefundListViewController

#pragma mark - Get
- (NSMutableArray<OrderModel *> *)tableArray {
    if (!_tableArray) {
        _tableArray = [[NSMutableArray alloc] init];
    }
    return _tableArray;
}
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"退款订单";
    self.page = 1;
    [self.tableView clearExtendCellLine];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self getNetData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getNetData];
    }];
    [MBProgressHUD showHUDProgress:self.view animated:YES];   
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getNetData];
}
#pragma mark - action
- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
- (void)getNetFailure {
    if (_page != 1) {
        _page--;
    }
}
- (void)getNetSuccess:(NSDictionary *)dics {
    
    if ([dics[@"pagenation"][@"totalCount"] integerValue] > 0 && [dics[@"pagenation"][@"totalPage"] integerValue] > 1) {
        [self.tableView.mj_footer setHidden:NO];
    } else {
        [self.tableView.mj_footer setHidden:YES];
    }
    
    if (_page == 1) {
        [self.tableArray removeAllObjects];
        [self.tableView.mj_footer resetNoMoreData];
    }
    // 数据列表
    NSArray *arr = dics[@"data"];
    if (arr.count == 0 && _page != 1) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self getNetFailure];
        return;
    }
    for (NSDictionary *dataDic in arr) {
        OrderModel *model = [OrderModel mj_objectWithKeyValues:dataDic];
        [self.tableArray addObject:model];
    }
    [self.tableView reloadData];
}
#pragma mark - 网络请求
- (void)getNetData {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:PageSize forKey:@"pageSize"];
    
    // 状态（空字符串是全部，0已提交，1已付款，2已发货，3已签收，4已评价，7退款申请，8已退款
    [dic setObject:@(_page) forKey:@"pageNumber"];
    [dic setObject:@"7" forKey:@"omstatus"];
    
    // 获取订单列表接口
    [MYAFNetworking POST:URL_Get_Order parameters:dic success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [self endRefresh];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            NSDictionary *dics = responseObject;
            [self getNetSuccess:dics];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"data"] animated:YES];
            [self getNetFailure];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [self endRefresh];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
        [self getNetFailure];
    }];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tableArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray[section].orderson.count+2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *model = self.tableArray[indexPath.section];
    MineOrderTableViewCell *cell = [MineOrderTableViewCell refundListCellWith:tableView indexPath:indexPath count:model.orderson.count];
    [cell updateRefundListCellWith:model indexPath:indexPath];
    cell.orderBlock = ^(OrderButtonType type, NSInteger tag) {
        if (type == OrderButtonTypeRefundDetail) {
            //跳转到详情页
            NSLog(@"%ld",(long)tag);
            RefundDetailViewController *refundDetailVC = [[RefundDetailViewController alloc] init];
            refundDetailVC.detailModel = model;
            [self.navigationController pushViewController:refundDetailVC animated:YES];
        }
    };
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *model = self.tableArray[indexPath.section];
    RefundDetailViewController *refundDetailVC = [[RefundDetailViewController alloc] init];
    refundDetailVC.detailModel = model;
    [self.navigationController pushViewController:refundDetailVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *model = self.tableArray[indexPath.section];
    if (indexPath.row == model.orderson.count) {
        return 44;
    } else if (indexPath.row == model.orderson.count+1) {
        return 44;
    } else {
        return 100;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section < self.tableArray.count-1) {
        return 5;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 5)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
//主提示标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"暂无订单"
                                           attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0],
                                                        NSForegroundColorAttributeName:[UIColor grayTextColor]}];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    _page = 1;
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [self getNetData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
